<!-- 产奶记录 -->
<template>
    <!-- 筛选条件 -->
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="企业名称" v-if="companySelect">
            <el-select filterable clearable v-model="formInline.companyId">
                <el-option v-for="item in companyNameList" :label="item.name" :value="item.id" :key="item.id" />
            </el-select>
        </el-form-item>

        <el-form-item label="选择牛号">
            <el-select clearable filterable v-model="formInline.cowId">
                <el-option v-for="item in allComNameList" :label="`${item.code}-${item.breed}-${item.genderName}`" :value="item.id" />
            </el-select>
        </el-form-item>

        <el-form-item>
            <el-button icon="Search" @click="search">查询</el-button>
            <el-button v-if="uploadButton" type="primary" icon="Upload" @click="isShowUpload = true">导入</el-button>
            <el-button type="success" icon="Download">导出</el-button>
        </el-form-item>
    </el-form>

    <!-- 表格 -->
    <el-table :data="tableData" stripe border style="width: 100%;">
        <el-table-column prop="cowCode" label="牛号" width="350" fixed="left">
            <template #default="scope">
                <div style="display: flex;">
                    <span>{{ scope.row.cowCode }}</span>
                    <!-- 性别 -->
                    <el-tag v-if="scope.row.genderName == '公牛'" style="margin-left: auto;" effect="dark" type="primary" size="small">{{ scope.row.genderName }}</el-tag>
                    <el-tag v-if="scope.row.genderName == '母牛'" style="margin-left: auto;" effect="dark" type="danger" size="small">{{ scope.row.genderName }}</el-tag>
                    <!-- 品种 -->
                    <el-tag style="margin: 0 5px;" effect="dark" type="success" size="small">{{ scope.row.breed }}</el-tag>
                    <!-- 年龄 -->
                    <el-tag effect="dark" type="info" size="small">{{ scope.row.age }}</el-tag>
                </div>
            </template>
        </el-table-column>
        <el-table-column v-if="companyColumn" prop="companyName" label="企业名称" width="150" show-overflow-tooltip />
        <el-table-column prop="lactationBatch" label="泌乳批次" width="100" />
        <el-table-column prop="totalMilk" label="日产奶量/公斤" width="140" />
        <el-table-column prop="morningMilk" label="早/公斤" width="130" />
        <el-table-column prop="noonMilk" label="中/公斤" width="130" />
        <el-table-column prop="eveningMilk" label="晚/公斤" width="130" />
        <el-table-column prop="morningOxytocin" label="缩宫素注射量-早/IU" width="180" />
        <el-table-column prop="noonOxytocin" label="缩宫素注射量-中/IU" width="180" />
        <el-table-column prop="eveningOxytocin" label="缩宫素注射量-晚/IU" width="180" />
        <el-table-column prop="createUserName" label="记录人" width="100" />
        <el-table-column prop="recordDate" label="记录日期" width="130" />
        <el-table-column prop="remarks" label="记录备注" width="150" />
        <el-table-column label="操作" fixed="right" width="180">
            <template #default="scope">
                <el-button v-if="editButton" type="primary" icon="Edit" size="small" @click="editItem(scope.row.id)">编辑</el-button>
                <el-button v-if="deleteButton" type="danger" icon="Delete" size="small" @click="delItem(scope.row.id)">删除</el-button>
                <!-- <el-button type="info" icon="Warning" size="small" @click="abnormalHandler">数据异常</el-button> -->
            </template>
        </el-table-column>
    </el-table>
    <br/>
    <!-- 分页器 -->
    <el-pagination
        v-model:currentPage="pageNo"
        v-model:page-size="limit"
        :page-sizes="[15, 30, 50, 100]"
        :small="false"
        :background="true"
        layout="prev, pager, next, jumper, ->, sizes, total"
        :total="total"
        @current-change="getCowMilkPage"
        @size-change="sizeChange"
    />

    <!-- 导入-->
    <upload :isShowUpload="isShowUpload" @closeUploadDrawer="closeUploadDrawer"></upload>
    <!-- 编辑 -->
    <edit :isShowEdit="isShowEdit" :id="id" @closeEditDrawer="closeEditDrawer"></edit>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { reqGetCompanyNameList } from '@/api/company'
import { reqGetCowMilkPage, reqGetCowMilkDelete } from '@/api/daily'
import { reqCowMilkCowSelect } from '@/api/publicCowSelect'
import { ElMessageBox, ElMessage } from 'element-plus'
import upload from './upload/index.vue'
import edit from './edit/index.vue'
// 引入用户相关小仓库
import useUserStore from '@/store/modules/user'
let userStore = useUserStore()
const companyColumn = userStore.componentConfig.companyColumn // 企业名称列-是否显示
const companySelect = userStore.componentConfig.companySelect // 企业下拉-是否显示
const addButton = userStore.componentConfig.addButton // 新增按钮-是否显示
const deleteButton = userStore.componentConfig.deleteButton // 删除按钮-是否显示
const editButton = userStore.componentConfig.editButton // 编辑按钮-是否显示
const errorButton = userStore.componentConfig.errorButton // 数据异常按钮-是否显示
const uploadButton = userStore.componentConfig.uploadButton // 导入按钮-是否显示

// 分页器当前页码
let pageNo = ref(1)
// 每一页展示多少条数据
let limit = ref(15)
// 数据总条数
let total = ref(0)

// 导入弹窗，显示隐藏
const isShowUpload = ref(false)
// 关闭导入弹窗
const closeUploadDrawer = (val) => {
    isShowUpload.value = val
}

// 编辑弹窗，显示隐藏
const isShowEdit = ref(false)
// 关闭，编辑弹窗
const closeEditDrawer = (val) => {
    isShowEdit.value = val
    getCowMilkPage()
}

// 表单收集
const formInline = reactive({
    cowId: '', // 牛号
    companyId: '', // 企业id
    sortName: '', // 排序字段名称
    sortType: '', // 排序类型：ascending-正序、descending-倒序
})

// 点击查询按钮
const search = () => {
    pageNo.value = 1
    getCowMilkPage()
}

// 表格数据
const tableData = ref([])
// 获取产奶记录，列表数据
const getCowMilkPage = async () => {
    let res = await reqGetCowMilkPage(Object.assign(formInline, {
        pageNumber: pageNo.value, // 页号
        pageSize: limit.value // 每页数量
    }))
    if(res.status == 200){
        tableData.value = res.data.records
        total.value = res.data.totalRow // 赋值数据总条数
    }
}

// 获取牛号下拉列表
let allComNameList = ref([])
const cowMilkCowSelect = async () => {
    let res = await reqCowMilkCowSelect()
    allComNameList.value = res.data
    console.log('7788', res)
}

// 获取企业下拉列表数据
let companyNameList = ref([])
const getCompanyNameList = async () => {
    let res = await reqGetCompanyNameList()
    companyNameList.value = res.data
}

// 编辑编辑按钮
let id = ref(0)
const editItem = (num) => {
    isShowEdit.value = true
    id.value = num
}

// 删除某一行数据
const delItem = (num) => {
    ElMessageBox.confirm(
        '确定是否删除?',
        '提示',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }
    ).then(async () => {
        let res = await reqGetCowMilkDelete({
            id: num
        })
        if(res.status == 200){
            ElMessage({
                type: 'success',
                message: '删除成功'
            })
        }
        getCowMilkPage()
    })
}


// 异常按钮
const abnormalHandler = () => {
    ElMessageBox.prompt('请输入异常原因', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        closeOnClickModal: false,
        inputPattern: /^.+$/,
        inputErrorMessage: '请填写异常原因'
    }).then((res) => {
            console.log('异常原因为：', res.value)
    }).catch(() => {
            console.log('取消填写异常原因')
    })
}

// 分页的下拉菜单发生改变时触发
const sizeChange = () => {
    pageNo.value = 1
    getCowMilkPage()
}

// 组件挂载完毕
onMounted(() => {
    cowMilkCowSelect()
    getCowMilkPage()
    getCompanyNameList()
})
</script>

<style scoped lang="scss">
.demo-form-inline .el-input {
  --el-input-width: 220px;
}

.demo-form-inline .el-select {
  --el-select-width: 220px;
}
</style>